//
//  H_ChangeUserNickVC.m
//  Honey
//
//  Created by 贾亚涵 on 16/5/8.
//  Copyright © 2016年 贾亚涵. All rights reserved.
//

#import "H_ChangeUserNickVC.h"

@interface H_ChangeUserNickVC ()<UITextFieldDelegate>
{
    UIImageView * _centerBarIV;
    UILabel * _BarLabel;
    
    UITextField * _changeNickTF;
    UIButton * _changNickBtn;
    NSString * _RNickStr;
}

@end

@implementation H_ChangeUserNickVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatChangeNickView];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self infoNavigationBarBtn];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [_BarLabel removeFromSuperview];
    
    [_centerBarIV removeFromSuperview];
}

-(void)infoNavigationBarBtn
{
    _centerBarIV = [[UIImageView alloc] initWithFrame:CGRectMake(50, 10, 1, 20)];
    _centerBarIV.backgroundColor = [UIColor blackColor];
    _centerBarIV.alpha = 0.2f;
    [self.navigationController.navigationBar addSubview:_centerBarIV];
    
    _BarLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_centerBarIV.frame)+14, 10, 100, 20)];
    _BarLabel.text = @"修改昵称";
    _BarLabel.textColor = [UIColor whiteColor];
    [self.navigationController.navigationBar addSubview:_BarLabel];
}

-(void)creatChangeNickView
{
    _changeNickTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 104, Screen_Width-20, 40)];
    _changeNickTF.backgroundColor = [UIColor whiteColor];
    //关闭首字母自动大写
    _changeNickTF.autocapitalizationType = UITextAutocapitalizationTypeNone;
    //添加清除小标
    _changeNickTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    _changeNickTF.placeholder = @"请输入修改后的昵称";
    _changeNickTF.borderStyle = UITextBorderStyleRoundedRect;
    [_changeNickTF addTarget:self action:@selector(RLTFChanged:) forControlEvents:UIControlEventEditingChanged];
    _changeNickTF.delegate = self;
    [self.view addSubview:_changeNickTF];
    
    _changNickBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(_changeNickTF.frame)+30, Screen_Width-20, 30)];
    [self.view addSubview:_changNickBtn];
    _changNickBtn.layer.cornerRadius = 8;
    [_changNickBtn setTitle:@"确认修改" forState:UIControlStateNormal];
    _changNickBtn.backgroundColor = [UIColor orangeColor];
    [_changNickBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_changNickBtn addTarget:self action:@selector(ChangNickBtn:) forControlEvents:UIControlEventTouchUpInside];
}

//对注册信息的限制
-(void)RLTFChanged:(UITextField *)sender
{
    if (_changeNickTF.text.length <= 8)
    {
        _RNickStr = _changeNickTF.text;
    }else
    {
        _changeNickTF.text = _RNickStr;
    }
    
}

#pragma UITextFieldDelegate
//点击界面view 触发  收键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_changeNickTF resignFirstResponder];
}

-(void)ChangNickBtn:(id)sender
{
    if (sender && [sender isKindOfClass:[UIButton class]])
    {
        //收起键盘
        [self changePackKeyboard];
        //调用提示框第三方
        MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.color = [UIColor orangeColor];
        
        //判断用户名是否为空
        if (_changeNickTF.text.length != 0)
        {
            //判断用户名的长度
            if (_changeNickTF.text.length >= 2 && _changeNickTF.text.length <= 8)
            {
                [hud hide:YES];
                
                [self checkNickFromD];
            }else
            {
                //设置为文本模式
                [hud setMode:MBProgressHUDModeText];
                [hud setLabelText:@"请输入2到8位昵称"];
                [hud hide:YES afterDelay:1];
            }
        }else
        {
            //设置为文本模式
            [hud setMode:MBProgressHUDModeText];
            [hud setLabelText:@"昵称为空，请输入修改后的昵称"];
            [hud hide:YES afterDelay:1];
        }
    }
}

-(void)checkNickFromD
{
    H_CoreDataDAL * dal1 = [H_CoreDataDAL new];
    //调用提示框第三方
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.color = [UIColor orangeColor];
    
    if (![dal1 checkRepeatedlyNick:_changeNickTF.text])
    {
        H_UserManager * model = [H_UserManager Instance];
        H_UserMemberModel * MM = model.userModel;
        [dal1 changeUserNickWithPhone:MM.userPhone Nick:_changeNickTF.text];
        //设置为文本模式
        [hud setMode:MBProgressHUDModeText];
        [hud setLabelText:@"修改成功"];
        [hud hide:YES afterDelay:1];
        [self performSelector:@selector(pop) withObject:nil afterDelay:1.1];
        
    }else
    {
        //设置为文本模式
        [hud setMode:MBProgressHUDModeText];
        [hud setLabelText:@"昵称已存在，请重新输入"];
        [hud hide:YES afterDelay:1];
    }
}

-(void)pop
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark- 收键盘
-(void)changePackKeyboard
{
    [_changeNickTF resignFirstResponder];
   
}

@end
